﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.BLL;
using Maticsoft.Model;

namespace Maticsoft.Web.Admin
{
    public partial class add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BLL.assessment ass = new BLL.assessment();
                GridView1.DataSource = ass.GetAllList();
                GridView1.DataBind();
                BLL.users u = new BLL.users();
                GridView2.DataSource = u.GetAllList();
                GridView2.DataBind();
                RB1.Attributes.Add("onclick", "change('RB1');");
                RB1.Attributes.Add("id", "RB1");
                RB2.Attributes.Add("onclick", "change('RB2');");
                RB2.Attributes.Add("id", "RB2");
                RB3.Attributes.Add("onclick", "change('RB3');");
                RB3.Attributes.Add("id", "RB3");
                RB4.Attributes.Add("onclick", "change('RB4');");
                RB4.Attributes.Add("id", "RB4");
                RB5.Attributes.Add("onclick", "change('RB5');");
                RB5.Attributes.Add("id", "RB5");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            BLL.assessment ass = new BLL.assessment();
            Model.assessment assmod = new Model.assessment();
            assmod.assessmentname = TBname.Text;
            try
            {
                if (RB1.Checked)
                {
                    assmod.type = 1;
                    assmod.unit = 0;
                    assmod.perprice = 0;
                    assmod.imgprice = 0;
                    if (RB4.Checked)//篇
                    {
                        assmod.subtype = 2;
                        assmod.min = int.Parse(TextBox4.Text);
                        assmod.minunit = int.Parse(TextBox15.Text);
                        assmod.minpic = int.Parse(TextBox16.Text);
                        assmod.overunit = int.Parse(TextBox7.Text);
                        assmod.overprice = int.Parse(TextBox8.Text);
                        assmod.lessuint = int.Parse(TextBox18.Text);
                        assmod.lessprice = int.Parse(TextBox19.Text);
                    }
                    if (RB5.Checked)
                    {
                        assmod.subtype = 1;
                        assmod.min = int.Parse(TextBox5.Text);
                        assmod.minpic = int.Parse(TextBox6.Text);
                        assmod.overunit = int.Parse(TextBox9.Text);
                        assmod.overprice = int.Parse(TextBox10.Text);
                        assmod.overimgprice = int.Parse(TextBox12.Text);
                        assmod.lessuint=int.Parse(TextBox20.Text);
                        assmod.lessprice = int.Parse(TextBox21.Text);
                        assmod.lessimgprice = int.Parse(TextBox23.Text);
                    }
                    assmod.cycle = Convert.ToInt32( DropDownList1.SelectedValue);
                    assmod.basesalary = Convert.ToDecimal(TextBox17.Text);

                }
                else
                {
                    if (RB2.Checked)
                    {
                        assmod.type = 2;
                    }
                    if (RB3.Checked)
                    {
                        assmod.type = 3;
                    }
                    assmod.unit = int.Parse(TextBox2.Text);
                    assmod.perprice = Convert.ToDecimal(TextBox3.Text);
                    assmod.imgprice = Convert.ToDecimal(TextBox25.Text);
                    assmod.cycle = 1;
                    assmod.basesalary = 0;

                }
                ass.Add(assmod);
                Page.RegisterStartupScript("success", "<script>alert('添加成功！')</script>");
            }
            catch (Exception ex)
            {
                Page.RegisterStartupScript("success", "<script>alert('"+ex.Message+"！')</script>");
            }

        }

      

        protected void Button2_Click(object sender, EventArgs e)
        {
            BLL.users us = new BLL.users();
            Model.users usmod = new Model.users();
            usmod.assessmentid =int.Parse( DropDownList2.SelectedValue);
            if (TextBox13.Text != "" && TextBox24.Text != "")
            {
                usmod.name = TextBox13.Text;
                usmod.payinfo = TextBox26.Text;
                usmod.permisscode = DropDownList4.SelectedValue;
                usmod.permissname = DropDownList4.SelectedItem.Text;
                usmod.phone = TextBox27.Text;
                usmod.pwd = TextBox24.Text;
                us.Add(usmod);
                Page.RegisterStartupScript("success", "<script>alert('添加成功！')</script>");
            }
            else
                Page.RegisterStartupScript("success", "<script>alert('请填写必填字段！')</script>");
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            assbind();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            BLL.assessment ass = new BLL.assessment();
            int id=int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
            ass.Delete(id);
            assbind();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            BLL.assessment ass = new BLL.assessment();
            Model.assessment assmod=new Model.assessment();
            assmod.assessmentid=int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
           assmod.assessmentname= ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox1"))).Text.ToString().Trim();
           assmod.type = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox2"))).Text.ToString().Trim());
             assmod.unit = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox3"))).Text.ToString().Trim());
             assmod.perprice = decimal.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].FindControl("TextBox4"))).Text.ToString().Trim());
             assmod.imgprice = decimal.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].FindControl("TextBox5"))).Text.ToString().Trim());
             assmod.subtype = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].FindControl("TextBox6"))).Text.ToString().Trim());
             assmod.cycle = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].FindControl("TextBox7"))).Text.ToString().Trim());
             assmod.basesalary = decimal.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].FindControl("TextBox8"))).Text.ToString().Trim());
             assmod.min = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[9].FindControl("TextBox9"))).Text.ToString().Trim());
             assmod.minunit = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[10].FindControl("TextBox10"))).Text.ToString().Trim());
             assmod.minpic = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[11].FindControl("TextBox11"))).Text.ToString().Trim());
             assmod.overunit = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[12].FindControl("TextBox12"))).Text.ToString().Trim());
             assmod.overprice = decimal.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[13].FindControl("TextBox13"))).Text.ToString().Trim());
             assmod.overimgprice = decimal.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[14].FindControl("TextBox14"))).Text.ToString().Trim());
             assmod.lessuint = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[15].FindControl("TextBox15"))).Text.ToString().Trim());
             assmod.lessprice = decimal.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[16].FindControl("TextBox16"))).Text.ToString().Trim());
             assmod.lessimgprice = decimal.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[17].FindControl("TextBox17"))).Text.ToString().Trim());
             ass.Update(assmod);
             GridView1.EditIndex = -1;
             GridView1.DataSource = ass.GetAllList();
             GridView1.DataBind();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            assbind();
        }

        private void assbind()
        {
            BLL.assessment ass = new BLL.assessment();
            GridView1.DataSource = ass.GetAllList();
            GridView1.DataBind();
        }

        protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            BLL.users u = new BLL.users();
            int id=int.Parse(GridView2.DataKeys[e.RowIndex].Value.ToString());
            u.Delete(id);
            usersbind();
        }

        protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView2.PageIndex = e.NewPageIndex;
            usersbind();
        }

        private void usersbind()
        {
            BLL.users u = new BLL.users();
            GridView2.DataSource = u.GetAllList();
            GridView2.DataBind();
        }

        protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView2.EditIndex = -1;
            usersbind();
        }

        protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView2.EditIndex = e.NewEditIndex;
            usersbind();
        }

        protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            BLL.users u = new BLL.users();
            Model.users umod = new Model.users();
            umod.userid = int.Parse(GridView2.DataKeys[e.RowIndex].Value.ToString());
            umod.permisscode = ((Label)(GridView2.Rows[e.RowIndex].FindControl("Label2"))).Text.ToString().Trim();
            umod.permissname = ((Label)(GridView2.Rows[e.RowIndex].FindControl("Label3"))).Text.ToString().Trim();
            umod.assessmentid = int.Parse(((TextBox)(GridView2.Rows[e.RowIndex].FindControl("TextBox2"))).Text.ToString().Trim());
            umod.name = ((TextBox)(GridView2.Rows[e.RowIndex].FindControl("TextBox4"))).Text.ToString().Trim();

            umod.pwd = ((TextBox)(GridView2.Rows[e.RowIndex].FindControl("TextBox5"))).Text.ToString().Trim();
            umod.phone = ((TextBox)(GridView2.Rows[e.RowIndex].FindControl("TextBox6"))).Text.ToString().Trim();

            umod.payinfo = ((TextBox)(GridView2.Rows[e.RowIndex].FindControl("TextBox7"))).Text.ToString().Trim();
           u.Update(umod);
            GridView2.EditIndex=-1;
            usersbind();
        }
        
    }
}